﻿using System;
using System.Data.SqlClient;

namespace DALHelper.DAL
{
    public class DALManager : IDisposable
    {
        private SqlConnection m_connection;

        #region properties
        public RoomCatDAL RoomCatDAL
        {
            get;
            set;
        }

        public RoomDAL RoomDAL
        {
            get;
            set;
        }

        public TransactionDAL TransactionDAL
        {
            get;
            set;
        }

        public TransDetailDAL TransDetailDAL
        {
            get;
            set;
        }

        public UserDAL UserDAL
        {
            get;
            set;
        } 
        #endregion

        /// <summary>
        /// Construct DAL with all of default parameters.
        /// </summary>
        public DALManager() : this("localhost", "QLDatPhong", "sa", "123456")
        {
        }

        /// <summary>
        /// Construct all of DAL's classes.
        /// </summary>
        /// <param name="sServerAddr">IP address of SQL server.</param>
        /// <param name="sUserName">Username, log into SQL server. It'll be used to construct the Connection string of SqlConnection.</param>
        /// <param name="sPassword">Password, log into SQL server</param>
        public DALManager(string sServerAddr, string sDatabase, string sUserName, string sPassword)
        {
            string sConnection = "Data Source=" + sServerAddr + ";Initial Catalog=" + sDatabase + ";User ID=" + sUserName + ";Password=" + sPassword;
            m_connection = new SqlConnection(sConnection);

            RoomCatDAL = new RoomCatDAL(m_connection);
            RoomDAL = new RoomDAL(m_connection);
            TransactionDAL = new TransactionDAL(m_connection);
            TransDetailDAL = new TransDetailDAL(m_connection);
            UserDAL = new UserDAL(m_connection);
        }

        #region IDisposable Members

        public void Dispose()
        {
            if(m_connection.State != System.Data.ConnectionState.Closed)
            {
                m_connection.Close();
            }
            m_connection.Dispose();
        }

        #endregion
    }
}
